Welcome back.
This lecture is going to go over all the mathematical operators available in Lua as well as the math
library.
You should already know the basics of math such as adding, subtracting, dividing, multiplying and
exponentiating numbers.
Here you can see all of the different operators responsible for those actions, such as adding, subtracting,
dividing, multiplying, exponentiating.
And then we also know about the modulus operator, which calculates how many times a number can fit
into another number and returns the remainder.
So for example.
If I were to divide seven by three using the modulus operator, we know that we can fit three into seven
two times, and the remainder left over would be the value of one.
Same thing if we did six modules two.
We know that two can fit into six three times.
And because there's not going to be any remainder, the leftover is zero.
And that's what's returned by our modulus operator.
We also have these other operators here that are specific to luoyue.
And the one that we saw previously was this one where we can add a number to itself.
So if we had a variable that stored the value of one instead of doing, let's say a is equal to a plus
one, we can just do a plus equal one, and that achieves the same effect.
We can also do the same thing with subtracting a number by itself.
We can do the same thing with division, multiplication, exponents, and even the modulus operator.
Let's go ahead and take a look at these mathematical operators in action.
I'm going to create a variable called a and just store the value of two.
And then what I'm going to do is put a bunch of print statements here.
And I'm just going to print something like a plus one.
And then I'm going to print out a minus one.
We can print out a divided by two.
We can print out a multiplied by two.
We can print out, let's say, a to the power of four, and then we can print out a modulus to.
What do you think's going to print out?
Well, let's find out if we hit run.
As you can see, a plus one is equal to three.
A minus one is equal to one.
A divided by two is equal to one.
A times two is equal to four, a to the power of four is 16, and then two fits into two one times,
so there's no remainder left over, which means we return or print out the value of zero here.
Now, just like regular mathematics, the interpreter follows the order of operations, which is usually
referred to as Pemdas or Bodmas, which either stands for parentheses, exponents, multiplication,
division, and then addition or subtraction, and bodmas stands for brackets, orders, Division, multiplication,
addition and subtraction.
Both of these acronyms mean the same thing.
So the first things that get operated on are the things that are inside of parentheses, and then the
next are anything that have exponents.
And then after those is going to be multiplication and division.
And then finally the last ones that get operated on are addition and subtraction.
To demonstrate the order of operations.
What I'm going to do is print into the console.
We're going to take a and we're going to wrap our a value inside of parentheses.
Inside of these parentheses I want to add the value of let's say three.
And then I want to raise this to the power of two.
And then afterwards we can subtract this by, let's say, nine divided by three.
Now if the interpreter follows the order of operations, then the value that should get printed into
the console should be the value of 22.
So we can go ahead and hit run.
And there we go.
We get the value of 22 as we expect, proving that the interpreter follows the order of operations.
Now, the last thing I want to talk about here is going to be the math library.
And this gives us access to a bunch of functions that aren't implemented as operators, such as getting
the absolute value of a number, the square root of a number, and many other trigonometric functions.
Now, I'm not going to be your math professor today, so I'm not going to go into much depth about all
of these mathematical functions, as that would take way too long and you probably won't remember any
of it.
However, the math library also has some functions that give us, for example, the smallest number
that is greater than or equal to a number we specify, which is useful for floats.
We have other functions to get the largest number that is smaller than or equal to a number we specify.
We can use some other functions which are called math.max.
Which will return the largest number out of the numbers we pass to the function.
We also have math.min, which will return the smallest number out of the numbers we pass to the function,
and there's a whole bunch of other ones.
So we go ahead and index the math library by typing out math.
As you can see, there's just a whole bunch of different mathematical functions, trigonometric functions,
and just a bunch of stuff that would take way too long to learn about.
But they're all here inside of the math library for us to use.
Specifically here, I want to focus on the math dot random function that we took a look at earlier,
and this function returns by default a pseudo random number between 0 and 1.
When we do not specify a range or pass a range to the function, they're called pseudo random numbers,
because numbers generated by a computer are never truly random, and this is because computers are awful
at generating random numbers.
Now we can also specify a seed for the random function, which means that the number that gets returned
by this function is based on the seed.
A seed will always return the same number, just like how a seed in a video game like Minecraft will
always return the same world.
To demonstrate this, I'm going to create a for loop that loops five times.
So we can do for I is equal to one.
And we want to loop five times.
And what we want to do is we want to set the math dot random seed equal to the current index.
And then we want to print out what gets returned by our math dot random function.
Now, since this math notrillionandom function is going to return a value based on the seed, every
single time we execute this loop, we should expect the same number to get printed out through each
iteration of this loop.
So with a random seed of one, any time we execute this math dot random function, as long as it has
a seed of one, it's always going to return the same number.
So if we run our game here.
As you can see, we get five different numbers based on those seeds from 1 to 5.
And then if we go ahead and stop the game and then run our game again.
As you can see, we get the exact same numbers printed out in the console, and we can keep stopping
the game and rerunning the game over and over again, because these will always print the same numbers
out.
And that's because those numbers are based on the seed that we pass here.
If we do not want to specify a seed, then we can just go ahead and delete this.
And our Math.random function will continue to generate random numbers inside of the console.
So if we hit run.
As you can see, we get a bunch of random numbers between 0 and 1, and then if we hit stop and then
run the test again.
Here we get a complete different set of random numbers.
And one last time, if we hit stop and run again.
We get another set of random numbers.
All righty.
That is all from me for this lecture.
I hope you enjoyed and I will see you in the next one.
Happy scripting.